<?php
/**
 * @author    Medard <pingguoli@gmail.com>
 * @date      2019/3/6 17:16
 * @copyright Copyright 2018 - 2019
 *  
 *  
 * @version   1.0
 */

namespace App\Http\Controllers\Wechat;


use App\Models\User;
use App\Support\ChunkUpload;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\Rule;

/**
 * Class ProfileController
 * @package App\Http\Controllers\Admin
 */
class ProfileController extends BaseController
{
    /**
     * 个人中心
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
     */
    public function index(Request $request)
    {
        $user = $this->getUser();
        if (empty($user)) {
            return back()->with('error', __('The page you requested was not found'));
        }
        $title = '个人中心';

        return frontendView('profile.index', compact('user', 'title'));
    }

    /**
     * 编辑个人中心
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
     */
    public function edit(Request $request)
    {
        /**
         * @var $user Admin
         */
        $user = $this->getUser();
        if (empty($user)) {
            return back()->with('error', __('The page you requested was not found'))->withInput();
        }

        if ($request->isMethod('POST')) {

            $this->validate($request, [
                'phone' => 'nullable|min:11|max:11',
            ], [], [
                'phone' => __('Mobile'),
            ]);

            $data = $request->only([ 'phone']);

            if ($user->update($data)) {
                return redirect()->route('wechat.profile')->with('success', __('Edit success'));
            }

            return back()->with('error', __('Edit failed'))->withInput();

        } else {
            $title = '编辑个人中心';
            return frontendView('profile.edit', compact('user', 'title'));
        }
    }

    /**
     * 更改密码
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
     */
    public function password(Request $request)
    {
        /**
         * @var $user Admin
         */
        $user = $this->getUser();
        if (empty($user)) {
            return back()->with('error', __('The page you requested was not found'))->withInput();
        }

        if ($request->isMethod('POST')) {

            $this->validate($request, [
                'orig_password' => 'required',
                'password' => 'required|confirmed',
            ], [], [
                'orig_password' => '原密码',
                'password' => __('Password'),
                'password_confirmation' => __('Confirm Password'),
            ]);

            $origPassword = $request->input('orig_password');
            if (!Hash::check($origPassword, $user->password)) {
                return back()->withErrors(['orig_password' => '原密码不正确'])->withInput();
            }

            $password = $request->input('password');
            $password = bcrypt($password);

            if ($user->update(['password' => $password])) {
                return redirect()->route('wechat.profile.password')->with('success', __('Edit success'));
            }

            return back()->with('error', __('Edit failed'))->withInput();

        } else {
            $title = '更改密码';
            return frontendView('profile.password', compact('user', 'title'));
        }
    }
}